package com.david.musicapp.adapter

import android.graphics.Color
import android.graphics.drawable.AnimationDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.david.musicapp.MusicApplication
import com.david.musicapp.PlayService.isPlaying
import com.david.musicapp.R
import com.david.musicapp.bean.Song
import com.david.musicapp.utils.SongHelper

class MusicsAdapter : RecyclerView.Adapter<MusicsAdapter.MusicHolder>(),
    View.OnClickListener {

    private val selectColor = Color.parseColor("#139EFF")
    private val defaultTitleColor = Color.parseColor("#0C0C0C")
    private val defaultSingerColor = Color.parseColor("#6D6D6D")
    private val defaultIndexColor = Color.parseColor("#a3a3a3")
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MusicHolder {
        return MusicHolder(
            LayoutInflater.from(MusicApplication.instance()).inflate(
                R.layout.item_song_list,
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: MusicHolder, position: Int) {
        holder.index.text = (position + 1).toString()
        holder.title.text = SongHelper.getInstance().getList()[position].getName() ?: ""
        holder.singer.text = SongHelper.getInstance().getList()[position].singer ?: ""

//            holder.img.setImageBitmap(SongHelper.getInstance()?.getList()?.get(position)?.albumImgBitmap)

        if (position == this.position) {
            holder.playLabel.visibility = View.VISIBLE
//            if (isPlaying) {
//                holder.animDrawable.start()
//            } else {
//                holder.animDrawable.stop()
//            }
            holder.playLabel.setImageResource(R.mipmap.gaj)

            holder.title.setTextColor(selectColor)
            holder.singer.setTextColor(selectColor)
        } else {
            holder.playLabel.visibility = View.GONE
//            holder.animDrawable.stop()
            holder.playLabel.setImageDrawable(null)

            holder.title.setTextColor(defaultTitleColor)
            holder.singer.setTextColor(defaultSingerColor)
            holder.index.setTextColor(defaultIndexColor)
        }

        holder.root.tag = position
    }

    override fun getItemCount(): Int {
        return SongHelper.getInstance().getList().size
    }

    var position: Int = 0
    fun setCurrent(songId: Long) {
        for (index in SongHelper.getInstance().getList().indices) {
            if (songId == SongHelper.getInstance().getList()[index].id) {
                val lastPosition = position
                position = index
                notifyItemChanged(lastPosition)
                notifyItemChanged(index)
                return
            }
        }
    }

    fun setCurrent(position: Int) {
        val lastPosition = this.position
        this.position = position

        notifyItemChanged(lastPosition)
        notifyItemChanged(this.position)
    }

    fun playStateChange() {
        isPlaying = SongHelper.getInstance().isPlaying
        notifyItemChanged(position)
    }


    inner class MusicHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var index: TextView = itemView.findViewById(R.id.music_index)
        var title: TextView = itemView.findViewById(R.id.item_music_title)
        var singer: TextView = itemView.findViewById(R.id.item_music_singer)
//        var img: ImageView = itemView.findViewById(R.id.item_music_img)
        var playLabel: ImageView = itemView.findViewById(R.id.item_music_play_label)
//        var animDrawable: AnimationDrawable = playLabel.drawable as AnimationDrawable
        var root: View = itemView

        init {
            root.setOnClickListener(this@MusicsAdapter)
        }
    }


    override fun onClick(p0: View?) {
        if (onMusicItemClickListener != null) {
            if (p0 != null && position != p0.tag) {
                onMusicItemClickListener.onMusicItemClick(p0.tag as Int, SongHelper.getInstance()
                    .getList()[p0.tag as Int]
                )
            }
        }
    }

    lateinit var onMusicItemClickListener:OnMusicItemClickListener

    interface OnMusicItemClickListener{
        fun onMusicItemClick(position: Int, song:Song)
    }
}